package com.dmjj.cad.controller;

import com.dmjj.cad.dto.CategoryDTO;
import com.dmjj.cad.dto.FloorplanDTO;
import com.dmjj.cad.dto.GraphDTO;
import com.dmjj.cad.service.FloorplanService;
import com.dmjj.cad.vo.FloorplanInsertVO;
import com.dmjj.cad.vo.FloorplanQueryVO;
import com.dmjj.cad.vo.FloorplanUpdateVO;
import com.dmjj.cad.vo.FloorplanVO;
import com.dmjj.cadcommon.base.BaseController;
import com.dmjj.cadcommon.result.ResultEnum;
import com.dmjj.cadcommon.result.ResultObject;
import com.dmjj.cadcommon.result.ResultPageObject;
import com.dmjj.cadcommon.util.ListCopyToVOUtil;
import com.dmjj.cadcommon.util.PaginationVOUtil;
import com.dmjj.cadcommon.vo.ResultPageVO;
import com.dmjj.cadcommon.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description 平面图管理表控制器
 * @Author zhangxue
 * @Date 2019-04-19 10:39:23
 */

@RestController
@Api(tags = "平面图管理表控制器")
@RequestMapping("floorplan")
public class FloorplanController extends BaseController {

    private static final Log LOGGER = LogFactory.getLog(FloorplanController.class);

    @Autowired
    private FloorplanService service;
    @Autowired
    private ResultObject resultObject;
    @Autowired
    private ResultPageObject resultPageObject;

    /**
	 * 平面图管理表列表
     * @Author zhangxue
     * @Date 2019-04-19 10:39:23
	 * @Param
	 * @Return
	 */
    @ApiOperation(value = "平面图管理表列表", response = FloorplanVO.class)
    @PostMapping("list")
    public ResultPageVO<Object> list4Page(
            FloorplanQueryVO entity,
            @ApiParam(value = "当前页", required = true) @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @ApiParam(value = "每页显示条数", required = true) @RequestParam(value = "numPerPage", defaultValue = "15") Integer pageSize
    ){
        try {
            FloorplanDTO entityDTO = new FloorplanDTO();
            BeanUtils.copyProperties(entity, entityDTO);
            List<FloorplanDTO> list = service.list4Page(entityDTO, pageNum, pageSize);
            List<FloorplanDTO> list1 = service.listByCondition(entityDTO);
            Integer total = list1.size();
            Integer totalPage = total/pageSize;
            if(total%pageSize!=0){
                totalPage += 1;
            }
            Boolean hasNext = false;
            if(pageNum!=null&&pageNum<totalPage){
                hasNext = true;
            }
            return resultPageObject.success(ResultEnum.SUCCESS.getCode(),ResultEnum.SUCCESS.getMsg(),pageNum,totalPage,pageSize,total,hasNext,list);
        }catch (Exception e) {
            e.printStackTrace();
            return resultPageObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * 平面图管理表列表（无分页）
     * @Author zhangxue
     * @Date 2019-04-19 10:39:23
     * @Param
     * @Return
     */
    @ApiOperation(value = "平面图管理表列表（无分页）", response = FloorplanVO.class)
    @PostMapping("listByCondition")
    public ResultVO<Object> listByCondition(FloorplanVO entity){
        try {
            FloorplanDTO entityDTO = new FloorplanDTO();
            BeanUtils.copyProperties(entity, entityDTO);
            List<FloorplanDTO> list = service.listByCondition(entityDTO);
            return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), ListCopyToVOUtil.getListToVO(list, FloorplanVO.class));
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * 平面图管理表详细信息
     * @Author zhangxue
     * @Date 2019-04-19 10:39:23
     * @Param
     * @Return
     */
    @ApiOperation(value = "平面图管理表详细信息", response = FloorplanVO.class)
    @PostMapping("byid")
    public ResultVO<Object> queryOneByPk(@ApiParam(value = "id",required = true) @RequestParam(value = "id") Long id){
        try {
            FloorplanDTO entityDTO = service.queryOneByPk(id);
            FloorplanVO entity = new FloorplanVO();
            BeanUtils.copyProperties(entityDTO, entity);
            return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), entity);
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * 保存平面图管理表信息
     * @Author zhangxue
     * @Date 2019-04-19 10:39:23
     * @Param
     * @Return
     */
    @ApiOperation(value = "保存平面图管理表信息", response = ResultVO.class)
    @PostMapping("insert")
    public ResultVO<Object> save(FloorplanInsertVO entity) {
        try {
            if(entity.getAnchorPoint()==null){
                return resultObject.error(ResultEnum.FAIL.getCode(), "缺少 anchorpoint 字段");
            }
            if(entity.getBasicBorderColor()==null){
                return resultObject.error(ResultEnum.FAIL.getCode(), "缺少 basicbordercolor 字段");
            }
            if(entity.getBasicFillColor()==null){
                return resultObject.error(ResultEnum.FAIL.getCode(), "缺少 basicfillcolor 字段");
            }
            if(entity.getCategoryid()==null){
                return resultObject.error(ResultEnum.FAIL.getCode(), "缺少 categoryid 字段");
            }
            if(entity.getCode()==null){
                return resultObject.error(ResultEnum.FAIL.getCode(), "缺少 code 字段");
            }
            if(entity.getName()==null){
                return resultObject.error(ResultEnum.FAIL.getCode(), "缺少 name 字段");
            }
            if(entity.getQueryBorderColor()==null){
                return resultObject.error(ResultEnum.FAIL.getCode(), "缺少 querybordercolor 字段");
            }
            if(entity.getQueryFillColor()==null){
                return resultObject.error(ResultEnum.FAIL.getCode(), "缺少 queryfillcolor 字段");
            }
            if(entity.getRentBorderColor()==null){
                return resultObject.error(ResultEnum.FAIL.getCode(), "缺少 rentbordercolor 字段");
            }
            if(entity.getRentFillColor()==null){
                return resultObject.error(ResultEnum.FAIL.getCode(), "缺少 RentFillColor 字段");
            }
            if(entity.getSize()==null){
                return resultObject.error(ResultEnum.FAIL.getCode(), "缺少 size 字段");
            }
            if(entity.getStatus()==null){
                return resultObject.error(ResultEnum.FAIL.getCode(), "缺少 status 字段");
            }

            FloorplanDTO entityDTO = new FloorplanDTO();
            BeanUtils.copyProperties(entity, entityDTO);
            if(service.save(entityDTO) > 0) {
                return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(),entityDTO);
            }else {
                return resultObject.error(ResultEnum.FAIL.getCode(), ResultEnum.FAIL.getMsg());
            }
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * 修改平面图管理表信息
     * @Author zhangxue
     * @Date 2019-04-19 10:39:23
     * @Param
     * @Return
     */
    @ApiOperation(value = "修改平面图管理表信息", response = ResultVO.class)
    @PostMapping("update")
    public ResultVO<Object> update(FloorplanUpdateVO entity) {
        try {
            if(entity.getAnchorPoint()==null){
                return resultObject.error(ResultEnum.FAIL.getCode(), "缺少 anchorpoint 字段");
            }
            if(entity.getBasicBorderColor()==null){
                return resultObject.error(ResultEnum.FAIL.getCode(), "缺少 basicbordercolor 字段");
            }
            if(entity.getBasicFillColor()==null){
                return resultObject.error(ResultEnum.FAIL.getCode(), "缺少 basicfillcolor 字段");
            }
            if(entity.getCategoryid()==null){
                return resultObject.error(ResultEnum.FAIL.getCode(), "缺少 categoryid 字段");
            }
            if(entity.getCode()==null){
                return resultObject.error(ResultEnum.FAIL.getCode(), "缺少 code 字段");
            }
            if(entity.getName()==null){
                return resultObject.error(ResultEnum.FAIL.getCode(), "缺少 name 字段");
            }
            if(entity.getQueryBorderColor()==null){
                return resultObject.error(ResultEnum.FAIL.getCode(), "缺少 querybordercolor 字段");
            }
            if(entity.getQueryFillColor()==null){
                return resultObject.error(ResultEnum.FAIL.getCode(), "缺少 queryfillcolor 字段");
            }
            if(entity.getRentBorderColor()==null){
                return resultObject.error(ResultEnum.FAIL.getCode(), "缺少 rentbordercolor 字段");
            }
            if(entity.getRentFillColor()==null){
                return resultObject.error(ResultEnum.FAIL.getCode(), "缺少 RentFillColor 字段");
            }
            if(entity.getSize()==null){
                return resultObject.error(ResultEnum.FAIL.getCode(), "缺少 size 字段");
            }
            if(entity.getStatus()==null){
                return resultObject.error(ResultEnum.FAIL.getCode(), "缺少 status 字段");
            }
            if(entity.getId()==null){
                FloorplanDTO entityDTO = new FloorplanDTO();
                BeanUtils.copyProperties(entity, entityDTO);
                if(service.save(entityDTO) > 0) {
                    return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(),entityDTO);
                }else {
                    return resultObject.error(ResultEnum.FAIL.getCode(), ResultEnum.FAIL.getMsg());
                }
            }else{
                FloorplanDTO entityDTO = new FloorplanDTO();
                BeanUtils.copyProperties(entity, entityDTO);
                if(service.edit(entityDTO) > 0) {
                    return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg());
                }else {
                    return resultObject.error(ResultEnum.FAIL.getCode(), ResultEnum.FAIL.getMsg());
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * 删除平面图管理表信息
     * @Author zhangxue
     * @Date 2019-04-19 10:39:23
     * @Param
     * @Return
     */
    @ApiOperation(value = "删除平面图管理表信息", response = ResultVO.class)
    @PostMapping("delete")
    @Transactional
    public ResultVO<Object> delete(@ApiParam(value = "id",required = true) @RequestParam(value = "id") Long id){
        try {
            if(service.delete(id) > 0) {
                //通过平面图id查询下属所有元素id并拼接字符串
                List<GraphDTO> GraphDTOList = service.selectGraphIdByFloorPlanId(id);
                String GraphIdsStr = "";
                for(int j=0;j<GraphDTOList.size();j++){
                    Integer tempId = GraphDTOList.get(j).getId();
                    GraphIdsStr += tempId +",";
                }
                GraphIdsStr = GraphIdsStr.substring(0,GraphIdsStr.length()-1);
                Map<String,String> tempMap = new HashMap<String,String>();
                tempMap.put("GraphIdsStr",GraphIdsStr);
                //通过所有元素id拼接的字符串删除所有子元素
                Integer g = service.deleteGraphSonByGraphIds(tempMap);
                if(g>=0){
                    //子元素删除成功后删除元素
                    int i = service.deleteGraphByFloorplanId(id);
                    if(i>=0){
                        return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg());
                    }else{
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                        return resultObject.success(ResultEnum.FAIL.getCode(), "删除元素失败");
                    }
                }else{
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return resultObject.success(ResultEnum.FAIL.getCode(), "删除子元素失败");
                }


            }else {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return resultObject.success(ResultEnum.FAIL.getCode(), "删除平面图失败");
            }
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

}